<template>
  <view class="licenses">
    <!-- 许可证列表 -->
    <view class="license-list" v-if="licenses.length">
      <view class="license-card" v-for="(license, index) in licenses" :key="index">
        <view class="license-header">
          <text class="license-type">{{license.type}}</text>
          <text :class="['status', license.status]">{{statusText[license.status]}}</text>
        </view>
        <view class="license-content">
          <view class="info-item">
            <text class="label">申请日期：</text>
            <text class="value">{{formatDate(license.applyDate)}}</text>
          </view>
          <view class="info-item">
            <text class="label">许可编号：</text>
            <text class="value">{{license.number || '待生成'}}</text>
          </view>
          <view class="info-item">
            <text class="label">有效期至：</text>
            <text class="value">{{license.expireDate ? formatDate(license.expireDate) : '待定'}}</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 空状态 -->
    <view class="empty-state" v-else>
      <image src="/static/empty-license.png" mode="aspectFit" />
      <text>暂无许可证记录</text>
    </view>

    <!-- 申请按钮 -->
    <view class="apply-btn" @click="applyLicense">
      <uni-icons type="plusempty" size="24" color="#fff" />
      <text>申请许可证</text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      licenses: [],
      statusText: {
        pending: '审核中',
        approved: '已通过',
        rejected: '已驳回',
        expired: '已过期'
      }
    }
  },
  methods: {
    formatDate(timestamp) {
      if (!timestamp) return ''
      const date = new Date(timestamp)
      return date.getFullYear() + '-' + String(date.getMonth() + 1).padStart(2, '0') + '-' + String(date.getDate()).padStart(2, '0')
    },
    applyLicense() {
      uni.navigateTo({
        url: '/pages/license-apply/license-apply'
      })
    },
    async loadLicenses() {
      try {
        const res = await uniCloud.callFunction({
          name: 'getLicenses',
          data: {}
        })
        if (res.result.code === 200) {
          this.licenses = res.result.data
        }
      } catch (error) {
        uni.showToast({
          title: '加载失败',
          icon: 'none'
        })
      }
    }
  },
  onShow() {
    this.loadLicenses()
  }
}
</script>

<style lang="scss">
.licenses {
  min-height: 100vh;
  background: #f5f5f5;
  padding-bottom: 120rpx;
}

.license-list {
  padding: 20rpx;

  .license-card {
    background: #fff;
    border-radius: 16rpx;
    padding: 20rpx;
    margin-bottom: 20rpx;

    .license-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20rpx;
      padding-bottom: 20rpx;
      border-bottom: 1px solid #f5f5f5;

      .license-type {
        font-size: 32rpx;
        font-weight: bold;
        color: #333;
      }

      .status {
        padding: 4rpx 16rpx;
        border-radius: 20rpx;
        font-size: 24rpx;

        &.pending {
          background: #e6f7ff;
          color: #1890ff;
        }

        &.approved {
          background: #f6ffed;
          color: #52c41a;
        }

        &.rejected {
          background: #fff2f0;
          color: #ff4d4f;
        }

        &.expired {
          background: #f5f5f5;
          color: #999;
        }
      }
    }

    .license-content {
      .info-item {
        display: flex;
        margin-bottom: 12rpx;

        &:last-child {
          margin-bottom: 0;
        }

        .label {
          width: 160rpx;
          color: #666;
          font-size: 28rpx;
        }

        .value {
          flex: 1;
          color: #333;
          font-size: 28rpx;
        }
      }
    }
  }
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 0;

  image {
    width: 200rpx;
    height: 200rpx;
    margin-bottom: 20rpx;
  }

  text {
    color: #999;
    font-size: 28rpx;
  }
}

.apply-btn {
  position: fixed;
  bottom: 40rpx;
  left: 50%;
  transform: translateX(-50%);
  background: #5cb85c;
  color: #fff;
  padding: 20rpx 60rpx;
  border-radius: 40rpx;
  display: flex;
  align-items: center;
  gap: 10rpx;
  box-shadow: 0 4rpx 12rpx rgba(92, 184, 92, 0.3);

  text {
    font-size: 28rpx;
  }
}
</style> 